﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using System.Configuration;

/// <summary>
/// Summary description for BaseClass
/// </summary>
/// 

using System.Web.Security;
using RTGS.Web.support;

namespace RTGS.Web
{
    public class BasePage : System.Web.UI.Page, IUserProfile
    {
        public BasePage()
        {
            this.Init += new EventHandler(BasePage_Init);

        }

        private void BasePage_Init(object sender, EventArgs e)
        {
            Response.CacheControl = "no-cache";
            Response.AddHeader("Pragma", "no-cache");
            Response.Expires = -1;
            
            MenuID = (Request.Params["menuid"] == null ? 0 : Convert.ToInt32(Request.Params["menuid"]));

            UserLoggedIn = "Anonymous";

            if (HttpContext.Current.User != null)
            {
                if (HttpContext.Current.User.Identity.IsAuthenticated)
                {
                    UserLoggedIn = this.Context.User.Identity.Name;

                    if (HttpContext.Current.User.Identity is FormsIdentity)
                    {
                        FormsIdentity id = HttpContext.Current.User.Identity as FormsIdentity;
                        FormsAuthenticationTicket ticket = id.Ticket;

                        string userData = ticket.UserData;
                        if (!string.IsNullOrEmpty(userData))
                        {
                            //string[] datas = userData.Split('|');
                            //UserID = Convert.ToInt32(userData); // UserID

                            UserID = Convert.ToInt32(userData);
                            


                        }
                    }
                }
            }

        }

        public string KemenhubConnectionString = ConfigurationManager.ConnectionStrings["Rtgs"].ConnectionString;

        public int MenuID
        {
            get;
            set;
        }

        public string UserLoggedIn
        {
            get;
            set;
        }

        public int UserID
        {
            get;
            set;
        }

        #region IUserProfile Members


        public string KodeEselon
        {
            get;
            set;
        }

        public string KodeWilayah
        {
            get;
            set;
        }

        public string KodeSatker
        {
            get;
            set;
        }


        public string DeskripsiEselon
        {
            get;
            set;
        }

        public string DeskripsiWilayah
        {
            get;
            set;
        }

        public string DeskripsiSatker
        {
            get;
            set;
        }

        #endregion
    }

    public abstract class BaseHandler : IHttpHandler, IUserProfile
    {

        public virtual bool IsReusable
        {
            get { return false; }
        }

        public virtual void ProcessRequest(HttpContext context)
        {
            UserLoggedIn = "Anonymous";

            if (HttpContext.Current.User != null)
            {
                if (HttpContext.Current.User.Identity.IsAuthenticated)
                {
                    UserLoggedIn = context.User.Identity.Name;

                    if (HttpContext.Current.User.Identity is FormsIdentity)
                    {
                        FormsIdentity id = HttpContext.Current.User.Identity as FormsIdentity;
                        FormsAuthenticationTicket ticket = id.Ticket;

                        string userData = ticket.UserData;
                        if (!string.IsNullOrEmpty(userData))
                        {

                            UserID = Convert.ToInt32(userData); // UserID

                           

                        }
                    }
                }
            }

        }

        public string KemenhubConnectionString = ConfigurationManager.ConnectionStrings["Rtgs"].ConnectionString;

        public string UserLoggedIn
        {
            get;
            set;
        }

        public int UserID
        {
            get;
            set;
        }

        #region IUserProfile Members


        public string KodeEselon
        {
            get;
            set;
        }

        public string KodeWilayah
        {
            get;
            set;
        }

        public string KodeSatker
        {
            get;
            set;
        }

       


        public string DeskripsiEselon
        {
            get;
            set;
        }

        public string DeskripsiWilayah
        {
            get;
            set;
        }

        public string DeskripsiSatker
        {
            get;
            set;
        }

        #endregion
    }

    public class BaseMasterPage : System.Web.UI.MasterPage, IUserProfile
    {
        public BaseMasterPage()
        {
            this.Init += new EventHandler(BaseMasterPage_Init);
        }

        protected void BaseMasterPage_Init(object sender, EventArgs e)
        {
            Response.CacheControl = "no-cache";
            Response.AddHeader("Pragma", "no-cache");
            Response.Expires = -1;

            MenuID = (Request.Params["menuid"] == null ? 0 : Convert.ToInt32(Request.Params["menuid"]));

            UserLoggedIn = "Anonymous";

            URLSimak = support.Helper.GetAppVirtualPath(this.Page) + "/";

            if (HttpContext.Current.User != null)
            {
                if (HttpContext.Current.User.Identity.IsAuthenticated)
                {
                    UserLoggedIn = this.Context.User.Identity.Name;

                    if (HttpContext.Current.User.Identity is FormsIdentity)
                    {
                        FormsIdentity id = HttpContext.Current.User.Identity as FormsIdentity;
                        FormsAuthenticationTicket ticket = id.Ticket;

                       

                        string userData = ticket.UserData;
                        if (!string.IsNullOrEmpty(userData))
                        {
                            string[] datas = userData.Split('|');
                            //UserID = Convert.ToInt32(userData); // UserID

                            UserID =  Convert.ToInt32(userData);
                          

                        }
                    }
                }
            }

        }

        public int MenuID
        {
            get;
            set;
        }

        public string UserLoggedIn
        {
            get;
            set;
        }

        public int UserID
        {
            get;
            set;
        }

        public string URLSimak
        {
            get;
            set;
        }

        #region IUserProfile Members


        public string KodeEselon
        {
            get;
            set;

        }

        public string KodeWilayah
        {
            get;
            set;
        }

        public string KodeSatker
        {
            get;
            set;
        }


        public string DeskripsiEselon
        {
            get;
            set;
        }

        public string DeskripsiWilayah
        {
            get;
            set;
        }

        public string DeskripsiSatker
        {
            get;
            set;
        }

        #endregion
    }

}